package com.lagou.edu.message.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lagou.edu.course.api.CourseRemoteService;
import com.lagou.edu.message.entity.Message;
import com.lagou.edu.message.mapper.MessageMapper;
import com.lagou.edu.message.server.PushServer;
import com.lagou.edu.message.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//import com.lagou.edu.user.api.UserRemoteService;
//import com.lagou.edu.user.api.dto.UserDTO;

/**
 * <p>
 * 消息通知表 服务实现类
 * </p>
 *
 * @author zlg
 * @since 2021-02-07
 */
@Slf4j
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {
    @Autowired
    CourseRemoteService courseRemoteService;
//    @Autowired
//    UserRemoteService userRemoteService;
    
    /*@Override
    @Transactional(rollbackFor = Exception.class)
    public List<Integer> saveMessage(Integer courseId) {
        log.info("saveMessage - courseId:{}",courseId);
        ValidateUtils.notNullParam(courseId);
        ValidateUtils.isTrue(courseId > 0, "课时id参数错误");
        
        CourseDTO courseDTO = courseRemoteService.getCourseById(courseId);
        ValidateUtils.isTrue(null != courseDTO, StringUtils.join("课程信息查询为空-courseId:",courseId));
        
        if(!courseDTO.getStatus().equals(1)) {
            log.warn("saveMessage - 课程id:{} 状态为：{} 不能发送消息",courseId, courseDTO.getStatus());
            return Lists.newArrayList();
        }
    
        Message saveMessage = new Message();
        saveMessage.setCourseId(courseId);
        saveMessage.setCourseLessonId(courseId);
        saveMessage.setCourseName(courseDTO.getCourseName());
        saveMessage.setCreateTime(LocalDateTime.now());
        saveMessage.setTheme("java");
        saveMessage.setUpdateTime(saveMessage.getCreateTime());
        saveMessage.setUserId(null);
        boolean saveRes = save(saveMessage);
    
        final Page<UserDTO> pagesUsers = userRemoteService.getPagesUsers(1, 10);
        List<Integer> res = new ArrayList<Integer>();
        final List<UserDTO> userDTOList = pagesUsers.getRecords();
        for (final UserDTO userDTO : userDTOList) {
            res.add(userDTO.getId());
        }
        return res;
    }*/
    
    @Override
    public void sendMessage(com.lagou.edu.message.api.dto.Message message) {
        if(null == message) {
            return;
        }
        PushServer.pushServer.push(message);
    }
}
